﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Linq.Expressions;
using System.Text;
using System.IO;
using LightSO.Entity;
using LightSO.ILibrary;
using LightSO.Extensions;

namespace LightSO.Business
{
    public partial class UserBll 
	{
        public User Get(int id)
        {
            var repo = SpringExtension.Ioc<IRepository<User>>();

            var info = repo.FindOne(o => o.Id == id);

            return info;
        }

        public void Save(User info)
        {
            if (info.Id < 1)
            {
                info.CreateOn = DateTime.Now;
                info.Save<User>(Entity.Enums.SaveMode.Insert);
                return;
            }
            info.Save<User>(Entity.Enums.SaveMode.Update, o => o.Id == info.Id);
        }

        public void Remove(int id)
        {
            SpringExtension.Ioc<IRepository<User>>().Remove(o => o.Id == id);
        }

        public List<User> Page(string pageIndex, string pageSize, string sort)
        {
            var repo = SpringExtension.Ioc<IRepository<User>>();

            return repo.FindPage(pageIndex.Parse<int>(), pageSize.Parse<int>(), null, o => (sort.Parse<bool>() ? o.CreateOn.Desc() : o.CreateOn.Asc())).Rows;
        }

        public List<User> List(string sort)
        {
            var repo = SpringExtension.Ioc<IRepository<User>>();

            return repo.All<User>(sort.Parse<bool>(), o=>o.CreateOn);
        }

        public List<User> Find(string keyword)
        {
            var repo = SpringExtension.Ioc<IRepository<User>>();
            if (string.IsNullOrWhiteSpace(keyword))
                return List("desc");

            return repo.Find(o => o.Name.Contains(keyword), o => o.Id.Desc());
        }
    }
}


